<template>
  <el-dialog :title="dialogTitle" :visible="visible" width="20%"   @close="onDialogClose">
    <el-form ref="form" :model="ruleForm" :rules="rules" label-width="80px">
      <el-form-item label="学科名称" prop="subjectNamess">
        <el-input v-model="ruleForm.subjectName"></el-input>
      </el-form-item>
    </el-form>
    <div style="margin-left:10px;font-weight:700">是否显示
    <el-switch
    style="margin-left:10px"
      v-model="ruleForm.isFrontDisplay"
      active-color="#13ce66"
      inactive-color="#ff4949"
    ></el-switch>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="$emit('input',false)">取 消</el-button>
      <el-button type="primary" @click="onSave">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { add, update } from '@/api/hmmm/subjects.js'
export default {
  model: {
    prop: 'visible'
  },
  props: {
    visible: {
      type: Boolean,
      required: true
    }
  },
  data () {
    return {
      ruleForm: {
        id: '',
        subjectName: '',
        isFrontDisplay: true
      },
      rules: {
        subjectName: [
          {
            required: true,
            message: '请输入学科名称',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  created () {
  },
  computed: {
    dialogTitle () {
      return this.ruleForm.id ? '编辑部门' : '新增部门'
    }
  },
  methods: {
    // 关闭弹层
    onDialogClose () {
      this.$emit('input', false)
      this.$refs.form.resetFields()
      this.ruleForm = {
        subjectName: '',
        isFrontDisplay: true,
        id: ''
      }
    },
    // 渲染点击修改之后拿到的名字
    async getDeptsInfo (row) {
      this.ruleForm = { ...row }
      if (this.ruleForm.isFrontDisplay) {
        this.ruleForm.isFrontDisplay = true
      }
    },
    // 点击确定
    async onSave () {
      try {
        await this.$refs.form.validate()
        // 编辑
        if (this.ruleForm.id) {
          await update(this.ruleForm)
          this.$emit('add-list')
          this.$message.success('修改成功')
        } else {
          // 添加
          await add(this.ruleForm)
          this.$emit('add-list')
          this.$message.success('添加成功')
        }
        this.$emit('input', false)
      } catch (err) {
        console.log(err)
      }
    }
  }
}
</script>

<style scoped lang="less"></style>
